package com.yidu.drug.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.model.QtSumModel;
import com.yidu.drug.model.QualityTestingModel;
import com.yidu.drug.service.QtSumModelService;
import com.yidu.drug.service.QualityTestingModelService;
import com.yidu.drug.service.impl.QualityTestingModelServicelmpl;

@Controller
@RequestMapping("/qualityTestingModel")
public class QualityTestingModelAction {
	@Resource
	private QualityTestingModelService qualityTestingModelService;
	@Resource
	private QtSumModelService qtSumModelService;
	
	/**
	 * 查询所有数据
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping("/findAllQualityTestingModel")
	@ResponseBody
	public Map<String, Object> findAllQualityTestingModel(HttpServletRequest request,HttpServletResponse response,String sumId,String qtPerson,String page,String limit) throws UnsupportedEncodingException{
		System.out.println("进来了=-=-=-=--=");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");


		System.out.println(sumId+"---------------------");

		System.out.println(qtPerson+"---------------------");
		
		Page pages = new Page();
		pages.setPage(Integer.valueOf(page));
		pages.setRows(Integer.valueOf(limit));

		QualityTestingModel qualityTestingModel = new QualityTestingModel();
		qualityTestingModel.setPage(pages);
		qualityTestingModel.setSumId(sumId);
		qualityTestingModel.setQtPerson(qtPerson);
		
		
		List<QualityTestingModel> list = qualityTestingModelService.findAllQualityTestingModel(qualityTestingModel);
		for(QualityTestingModel QualityTestingModel:list){
			System.out.println(QualityTestingModel.getQtPerson()+"========================");
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String str = sdf.format(QualityTestingModel.getQtTime());
			QualityTestingModel.setQtTimes(str);
		}
		System.out.println("到了这一步=-=-=-=-=-=-=-");
		Long count = qualityTestingModelService.findRows(qualityTestingModel);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", count);
		map.put("data", list);
		System.out.println("多撒us户的撒肯定撒");
		if(!Tools.isEmpty(list)){
			return map;
		}
		return null;
		
	}
	
	/**
	 * 增加管理人员数据
	 * @return 
	 * @return
	 */
	@RequestMapping("/addQualityTestingModel")
	public int add(QualityTestingModel qualityTestingModel,HttpSession session) throws IOException{
		return qualityTestingModelService.insertSelective(qualityTestingModel);
	}
	
	/**
	 * 计算
	 * @throws IOException 
	 */
	@RequestMapping("/update")
	@ResponseBody
	public String update(HttpSession session,QualityTestingModel qualityTestingModel) throws IOException{
		/*System.out.println(str+"====================");*/
		//return qualityTestingModelService.update(qualityTestingModel);
		/*Gson gson = new Gson(); 
		java.lang.reflect.Type listType = new TypeToken<ArrayList<QualityTestingModel>>(){}.getType();
		ArrayList<QualityTestingModel> list = gson.fromJson(str, listType);*/
		/*String strs = "";
		for (QualityTestingModel qualityTestingModel2 : list) {
			System.out.println("未通过数量:"+qualityTestingModel2.getNopassNum()+"总数量:"+qualityTestingModel2.getQtSumnum()+"========"
							  +"通过的数量:"+(qualityTestingModel2.getQtSumnum()-qualityTestingModel2.getNopassNum()));
			 strs = qualityTestingModelService.update(qualityTestingModel2);
		}
		return strs;*/
		System.out.println(qualityTestingModel.getQtId()+"  "+qualityTestingModel.getNopassNum()+"  "+qualityTestingModel.getQtSumnum());
		
		return qualityTestingModelService.update(qualityTestingModel);
	}
	
	/**
	 * 计算
	 * @throws IOException 
	 */
	@RequestMapping("/updateTwo")
	@ResponseBody
	public String updateTwo(HttpSession session,String sumId,Integer sumNopassNum,Integer sumQtSumnum) throws IOException{
		/*System.out.println(str+"====================");
		//return qualityTestingModelService.update(qualityTestingModel);
		Gson gson = new Gson(); 
		java.lang.reflect.Type listType = new TypeToken<ArrayList<QualityTestingModel>>(){}.getType();
		ArrayList<QualityTestingModel> list = gson.fromJson(str, listType);
		for (QualityTestingModel qualityTestingModel2 : list) {
			System.out.println("未通过数量:"+qualityTestingModel2.getNopassNum()+"总数量:"+qualityTestingModel2.getQtSumnum()+"========");
		
			 qualityTestingModelService.updateTwo(qualityTestingModel2);
			//return "123";
		}
		
		return "redirect:/pages/lsj/QtSumModelShowList.html";*/
		double d = (Double.valueOf(sumQtSumnum) - Double.valueOf(sumNopassNum)) / sumQtSumnum;
		java.math.BigDecimal bg = new java.math.BigDecimal(d);
		//进行四舍五入
		java.math.BigDecimal bgs = bg.setScale(2,BigDecimal.ROUND_HALF_UP);
		QtSumModel qtSumModel = new QtSumModel();
		qtSumModel.setSumId(sumId);
		qtSumModel.setSumRate(bgs);
		qtSumModel.setSumNopass(sumNopassNum);
		int rows = qtSumModelService.updateByPrimaryKeySelective(qtSumModel);
		if(rows > 0){
			return "ok";
		}else {
			return "no";
		}
	}
}
